import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
import scrollIntoView from 'scroll-into-view-if-needed';

export default function scrollToFirstError(
  errors: Record<string, ValidatedError>
) {
  const keys = Object.keys(errors);
  const firstError = errors[keys[0]];
  const id = firstError.field.replace(/[[.]/g, '_').replace(/\]/g, '');
  const ele = document.querySelector(`#${id}`);
  if (ele) {
    scrollIntoView(ele, {
      behavior: 'smooth',
    });
  }
}
